package com.example.myshopping3.controller;


import com.example.myshopping3.model.OrderParam;
import com.example.myshopping3.model.Result;
import com.example.myshopping3.service.CarService;
import com.example.myshopping3.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @author 路老师
 * @date 2025/7/22
 * 无精神，不编程！无乐趣，不编程！
 * “调试：与bug共舞的艺术。”
 */
@RestController
@RequestMapping("/car/orders")
public class OrderController {

    @Autowired
    private OrderService orderService; //注入订单业务逻辑

    @Autowired
    private CarService carService; //注入购物车业务逻辑

    //添加订单
    @PostMapping("/addOrder")
    public Result addOrder(@RequestBody OrderParam orderParam, HttpSession session) {

        System.out.println("orderParam.getOrders()"+orderParam.getOrders());
        System.out.println("orderParam.getOrderDescList()"+orderParam.getOrderDescList());
        //获取当前登陆者的userId
//        Object curuser = (CurUser)session.getAttribute("curUser");
        //将userId设置到订单中
        orderParam.getOrders().setUserId("99");
        //添加订单
        orderService.addOrder(orderParam.getOrders(), orderParam.getOrderDescList());
        //将已购买的商品从购物车中移除
        carService.removeBatchByIds(orderParam.getCarIds());

    //    return Result.success("订单已生成", null);  //我自己的
        //支付宝接口支付时修改
        return Result.success("订单已生成",orderParam.getOrders());
    }
}
/*

apipost中构造的测试数据

{
  "orders": {
    "orderId": "ORDER_123456",
    "userId": 90,
    "orderToName": "张三",
    "orderToAddress": "北京市海淀区",
    "orderToPhone": "13800138000",
    "orderStatus": 1,
    "orderTotalPrice": 299.99,
    "payStatus": 1
  },
  "orderDescList": [
    {"bookId": 1, "count": 2},  // 图书ID需真实存在
    {"bookId": 2, "count": 1}
  ],
  "carIds": [1947134637537071106, 1947139337749630977]  // 购物车项ID需真实存在
}
}
 */
